//
//  JJPictureInfiniteView.m
//  PictureInfinite
//
//  Created by jxf on 16/2/21.
//  Copyright © 2016年 JJ.sevn. All rights reserved.
//

#import "JJPictureInfiniteView.h"

@interface JJPictureInfiniteView ()<UIScrollViewDelegate>
@property(nonatomic, weak) UIScrollView * scrollView;
@property(nonatomic, weak) NSTimer * timer;
@property(nonatomic, weak) UIPageControl * pageControl;
@end

@implementation JJPictureInfiniteView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.bounces = NO;
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        for (int i = 0; i < 3; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.userInteractionEnabled = YES;
            [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidClicked:)]];
            [scrollView addSubview:imageView];
        }
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        pageControl.pageIndicatorTintColor = [UIColor purpleColor];
        pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        [self addSubview:pageControl];
        self.pageControl = pageControl;
        
        [self startTimer];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.scrollView.frame = self.bounds;
    CGFloat imageViewW = self.scrollView.frame.size.width;
    CGFloat imageViewH = self.scrollView.frame.size.height;
    self.scrollView.contentSize = CGSizeMake(imageViewW * self.scrollView.subviews.count, 0);
    int index = 0;
    for (UIImageView *imageView in self.scrollView.subviews) {
        imageView.frame = CGRectMake(imageViewW * index, 0, imageViewW, imageViewH);
        index ++;
    }
    CGFloat pageControlW = 100;
    CGFloat pageControlH = 30;
    self.pageControl.frame = CGRectMake(self.frame.size.width - pageControlW, self.frame.size.height - pageControlH, pageControlW, pageControlH);
    
    [self reloadSelf];
    NSLog(@"%s", __func__);
}

#pragma mark ------------------------------------------
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIImageView *curImageView = nil;
    CGFloat curImageViewMargin = MAXFLOAT;
    for (UIImageView *imageView in self.scrollView.subviews) {
        CGFloat margin = ABS(imageView.frame.origin.x - scrollView.contentOffset.x);
        if (margin < curImageViewMargin) {
            curImageViewMargin = margin;
            curImageView = imageView;
        }
    }
    self.pageControl.currentPage = curImageView.tag;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self reloadSelf];
    [self startTimer];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self reloadSelf];
}

#pragma mark ------------------------------------------
#pragma mark other
- (void)reloadSelf
{
    NSInteger curPage = self.pageControl.currentPage;
    for (int i = 0; i < self.scrollView.subviews.count; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        NSInteger imageIndex = 0;
        if (i == 1) {
            imageIndex = curPage;
        }else if (i == 0){
            imageIndex = curPage - 1;
        }else{
            imageIndex = curPage + 1;
        }
        
        if (imageIndex == -1) {
            imageIndex = self.images.count - 1;
        }else if (imageIndex == self.images.count){
            imageIndex = 0;
        }
        imageView.image = self.images[imageIndex];
        imageView.tag = imageIndex;
    }
    
    self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
}

- (void)startTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)nextPage
{
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + self.scrollView.frame.size.width, 0) animated:YES];
}

- (void)imageViewDidClicked:(UITapGestureRecognizer *)tap
{
    if ([self.delegate respondsToSelector:@selector(pictureInfiniteView:didClickedImageViewAtIndex:)]) {
        [self.delegate pictureInfiniteView:self didClickedImageViewAtIndex:tap.view.tag];
    }
}

- (void)setImages:(NSArray *)images
{
    _images = images;
    self.pageControl.numberOfPages = self.images.count;
}
@end
